package per.zqx.workQueue;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import per.zqx.utils.RabbitUtils;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * Consumer_fair_01
 *
 * @author Sa  2021-03-17 09:35
 */
public class Consumer_RoundRobin_01 {
    public static void main(String[] args) throws IOException, TimeoutException {
        final String QUEUE_NAME = "work_queue";
        Channel channel = RabbitUtils.getConnection().createChannel();
        channel.queueDeclare(QUEUE_NAME, true, false, false, null);
        DeliverCallback deliverCallback = ((consumerTag, message) -> {
            String msg = new String(message.getBody(), "utf-8");
            long deliveryTag = message.getEnvelope().getDeliveryTag();
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Consumer_fair_01 received a message : " + msg);
        });
        // 必须自动确认，否则信息将被多次消费
        boolean autoAck = true;
        channel.basicConsume(QUEUE_NAME,autoAck, deliverCallback, consumerTag -> {});
    }
}
